<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Box Flip Example</title>
    <style>
        .flip-container {
            perspective: 1000px;
            position: relative;
            margin-bottom: 50px; /* 为按钮留出空间 */
        }

        .flipper {
            transition: 0.6s;
            transform-style: preserve-3d;
            position: relative;
            transform-origin: center center; /* 确保翻转时盒子保持中心位置 */
        }

        .front, .back {
            backface-visibility: hidden;
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 100px;
        }

        .front {
            background-color: red;
            z-index: 2;
            transform: rotateY(0deg);
        }

        .back {
            background-color: blue;
            transform: rotateY(180deg);
        }

        .flipped .flipper {
            transform: rotateY(180deg);
        }

        #flip-button {
            position: absolute; /* 定位按钮在盒子的下方 */
            bottom: 0;
            left: 50%; /* 水平居中 */
            transform: translateX(-50%); /* 水平居中 */
        }
    </style>
</head>
<body>

<div class="flip-container">
    <div class="flipper">
        <div class="front">
            <p>正面</p>
        </div>
        <div class="back">
            <p>背面</p>
        </div>
    </div>
</div>

<button id="flip-button">翻转盒子</button>

<script>
    document.getElementById('flip-button').addEventListener('click', function() {
        var flipContainer = document.querySelector('.flip-container');
        flipContainer.classList.toggle('flipped');
    });
</script>

</body>
</html>
